/**
 *
 */
package com.elite.groovy.callback

import com.elite.groovy.util.EliteBean
import com.elite.groovy.util.Util

import net.sf.json.JSONObject
import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

/**
 * @author ThinkPad
 *
 */
class SendSmsXB implements ServiceBackInvoke {
    private static final Log log=LogFactory.getLog(SendSmsXB.class);
	private String charset = "UTF-8"; //设置字符集
	private int timeout = 100000; //设置请求超时时间
	private int enableSSLflag = 1;
    String doCallBack(EliteBean eliteBean, HttpServletRequest request, HttpServletResponse response) {
		log.debug("-----Begin SendXbSms----------");
        response.setHeader("Content-type", "text/html;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");
        Util u = new Util();
        String requestJSONStr =u.getRequestBody(request, "UTF-8");
		log.debug(requestJSONStr);
		//解析请求json
		JSONObject reqJson  = new JSONObject().fromObject(requestJSONStr);

		String documentoutGuid = UUID.randomUUID().toString();
		String mobile = reqJson.getString("mobile");
		String content = reqJson.getString("content");
		String handlebyId = reqJson.getString("handlebyId");
		String handlegroupId = reqJson.getString("handlegroupId");
        
		def smap = eliteBean.chaxun_sql("select url,th_code,dd_key,dd_use_id,edj_appkey from sysc_interface where urltype ='newsmsSend'", "dataSource");
        if (smap.size() == 0) {
            return
        };
        String url = smap[0].getAt("url");
        String appCode = smap[0].getAt("th_code");//账号编码
        String appSecret = smap[0].getAt("dd_key");//账号密码
        String bizType = smap[0].getAt("dd_use_id");//业务类型
        String orgCode = smap[0].getAt("edj_appkey");//机构代码

		try {


			noModelSend(eliteBean, documentoutGuid, url, mobile, content, appCode, appSecret, bizType, orgCode, handlebyId, handlegroupId);//非模板短信发送
			
			log.debug("本次发送续保短信成功");
			
			JSONObject  responseJson = new JSONObject();
			responseJson.put("retCode", "1");
			responseJson.put("retMessage", "success");
			return responseJson;
		} catch (Exception e) {
			log.error("ErrorMessage:", e);

			JSONObject  responseJson = new JSONObject();
			responseJson.put("retCode", "-1");
			responseJson.put("retMessage","failure");
			return responseJson;
		}

		
    }

	//无模板短信发送
    public boolean noModelSend(EliteBean eliteBean, String msgGuid, String url, String mobile, String content, String appCode, String appSecret, String bizType, String orgCode, String handlebyId,String handlegroupId) throws Exception {
        try {
            HashMap<String, Object> headMap = new HashMap<>();
            headMap.put("Content-Type", "application/json");
            JSONObject smsJson = new JSONObject();
            smsJson.put("appCode", appCode);
            smsJson.put("appSecret", appSecret);
            smsJson.put("mobile", mobile);
            smsJson.put("content", URLEncoder.encode(content, "UTF-8"));
            smsJson.put("isIm", 1);
            smsJson.put("chanType", 1);//0-普通长号，1-营销长号，2-客服短号
            smsJson.put("orgCode", orgCode);
            smsJson.put("bizType", bizType);
            String httpresult = Util.doHttpPostUseHead(url, smsJson.toString(), charset, timeout, enableSSLflag, headMap);
            JSONObject obj = JSONObject.fromObject(httpresult);
			log.debug("obj:"+obj);
            String msgId=obj.getString("smsId");
            if (obj.get("result") == 0) {
				eliteBean.update("insert into documentout (documentout_guid,documentfrom,doctype,status,trialcount,Trialcountlimit,Documentpath,Handleby_Id,Handlegroup_Id,Customer_Guid,Createdby,Createddate,Expectedstartdate,Documentto,msgid,LASTTRYDATETIME,MODIFIEDDATE,MODIFIEDBY,DOCUMENTTO2)values('"+msgGuid+"',1,3,1,1,1,'"+content+"','"+handlebyId+"','"+handlegroupId+"','','"+handlebyId+"',sysdate,sysdate,'"+mobile+"','"+obj.getString("smsId")+"',sysdate,sysdate,'SELITE','XB')", "dataSource");
                return true;
            } else {
				eliteBean.update("insert into documentout (documentout_guid,documentfrom,doctype,status,faildescription,trialcount,Trialcountlimit,Documentpath,Handleby_Id,Handlegroup_Id,Customer_Guid,Createdby,Createddate,Expectedstartdate,Documentto,msgid,LASTTRYDATETIME,MODIFIEDDATE,MODIFIEDBY,DOCUMENTTO2)values('"+msgGuid+"',1,3,5,'"+obj.getString("message")+"',1,1,'"+content+"','"+handlebyId+"','"+handlegroupId+"','','"+handlebyId+"',sysdate,sysdate,'"+mobile+"','"+obj.getString("smsId")+"',sysdate,sysdate,'SELITE','XB')", "dataSource");
                return false;
            }
        } catch (Exception e) {
            log.error("ErrorMessage:", e);
            return false;
        }
    }

}
